package com.cw.carwashcity.service.impl;

import com.cw.carwashcity.domain.DouArea;
import com.cw.carwashcity.mapper.DouAreaMapper;
import com.cw.carwashcity.service.DouAreaService;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class DouAreaServiceImpl implements DouAreaService {
    @Autowired
    private DouAreaMapper areaMapper;

    @Autowired
    private RedisService redisService;

    // Redis缓存key前缀
    private static final String PROVINCE_KEY = "area:provinces";
    private static final String CITY_KEY_PREFIX = "area:cities:";
    private static final String DISTRICT_KEY_PREFIX = "area:districts:";
    
    // 缓存过期时间（24小时）
//    private static final long CACHE_EXPIRE_TIME = 24;

    /**
     * 查询地区列表
     *
     * @param area 地区信息
     * @return 地区集合
     */
    @Override
    public List<DouArea> selectAreaList(DouArea area) {
        return areaMapper.selectAreaList(area);
    }

    /**
     * 查询地区信息
     *
     * @param areaId 地区ID
     * @return 地区信息
     */
    @Override
    public DouArea selectAreaById(Integer areaId) {
        return areaMapper.selectAreaById(areaId);
    }

    /**
     * 新增地区
     *
     * @param area 地区信息
     * @return 结果
     */
    @Override
    public int insertArea(DouArea area) {
        int rows = areaMapper.insertArea(area);
        if (rows > 0) {
            clearCache(area);
        }
        return rows;
    }

    /**
     * 修改地区
     *
     * @param area 地区信息
     * @return 结果
     */
    @Override
    public int updateArea(DouArea area) {
        int rows = areaMapper.updateArea(area);
        if (rows > 0) {
            clearCache(area);
        }
        return rows;
    }

    /**
     * 批量删除地区
     *
     * @param areaIds 需要删除的地区ID
     * @return 结果
     */
    @Override
    public int deleteAreaByIds(Integer[] areaIds) {
        int rows = areaMapper.deleteAreaByIds(areaIds);
        if (rows > 0) {
            // 清除所有缓存
            redisService.deleteObject(PROVINCE_KEY);
            redisService.deleteObject(redisService.keys(CITY_KEY_PREFIX + "*"));
            redisService.deleteObject(redisService.keys(DISTRICT_KEY_PREFIX + "*"));
        }
        return rows;
    }

    /**
     * 删除地区信息
     *
     * @param areaId 地区ID
     * @return 结果
     */
    @Override
    public int deleteAreaById(Integer areaId) {
        DouArea area = areaMapper.selectAreaById(areaId);
        int rows = areaMapper.deleteAreaById(areaId);
        if (rows > 0 && area != null) {
            clearCache(area);
        }
        return rows;
    }

    /**
     * 获取所有省份
     * @return
     */
    @Override
    public List<DouArea> getProvinces() {
        // 先从Redis获取
        List<DouArea> provinces = redisService.getCacheObject(PROVINCE_KEY);
        if (provinces != null) {
            return provinces;
        }
        
        // Redis没有则从数据库查询
        provinces = areaMapper.selectByParentId((short) 0);
        if (provinces != null) {
            // 存入Redis
            redisService.setCacheObject(PROVINCE_KEY, provinces);
        }
        return provinces;
    }

    /**
     * 获取城市
     * @param parentId
     * @return
     */
    @Override
    public List<DouArea> getCities(Short parentId) {
        String key = CITY_KEY_PREFIX + parentId;
        // 先从Redis获取
        List<DouArea> cities = redisService.getCacheObject(key);
        if (cities != null) {
            return cities;
        }
        
        // Redis没有则从数据库查询
        cities = areaMapper.selectByParentId(parentId);
        if (cities != null) {
            // 存入Redis
            redisService.setCacheObject(key, cities);
        }
        return cities;
    }

    /**
     * 获取区县
     * @param parentId
     * @return
     */
    @Override
    public List<DouArea> getDistricts(Short parentId) {
        String key = DISTRICT_KEY_PREFIX + parentId;
        // 先从Redis获取
        List<DouArea> districts = redisService.getCacheObject(key);
        if (districts != null) {
            return districts;
        }
        
        // Redis没有则从数据库查询
        districts = areaMapper.selectByParentId(parentId);
        if (districts != null) {
            // 存入Redis
            redisService.setCacheObject(key, districts);
        }
        return districts;
    }

    /**
     * 清除相关缓存
     */
    private void clearCache(DouArea area) {
        if (area.getParentId() == 0) {
            // 如果是省份，清除省份缓存和所有城市缓存
            redisService.deleteObject(PROVINCE_KEY);
            redisService.deleteObject(redisService.keys(CITY_KEY_PREFIX + "*"));
        } else {
            // 如果是城市，清除该省份下的城市缓存和所有区县缓存
            redisService.deleteObject(CITY_KEY_PREFIX + area.getParentId());
            redisService.deleteObject(redisService.keys(DISTRICT_KEY_PREFIX + "*"));
        }
    }
}
